package com.cheyun.netsalev3.view;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.cheyun.netsalev3.R;
import com.cheyun.netsalev3.bean.drive.ReserveInfo;
import com.cheyun.netsalev3.utils.FunctionUtils;
import com.cheyun.netsalev3.utils.api.ApiCallback;
import com.cheyun.netsalev3.utils.api.ApiClient;
import com.cheyun.netsalev3.utils.api.ApiException;
import com.cheyun.netsalev3.utils.api.BaseResp;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.yxing.ScanCodeActivity;

import androidx.appcompat.widget.AppCompatButton;
import androidx.lifecycle.Observer;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 */
public class MyScanActivity extends ScanCodeActivity {

    private AppCompatButton btnOpenFlash;

    private boolean isOpenFlash;

    public static boolean hasOpenScan = false;

    public static String qrcode = "";

    public static ReserveInfo reserveInfo;

    public static Context mainContext;

    @Override
    public int getLayoutId() {
        return R.layout.activity_myscan;
    }

    @Override
    public void initData() {
        super.initData();
        btnOpenFlash = findViewById(R.id.btn_openflash);

        btnOpenFlash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isOpenFlash = !isOpenFlash;
                setFlashStatus(isOpenFlash);
                btnOpenFlash.setText(isOpenFlash ? "关闭闪光灯" : "打开闪光灯");
            }
        });

//        LiveEventBus.get().with("showQuanyiConfirm",String.class).observe(this, new Observer<String>() {
//            @Override
//            public void onChanged(String s) {
//                doQuanyiCheck();
//            }
//        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        hasOpenScan = false;
        qrcode = "";
    }

    public void showCustomAlert(){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                AlertDialog.Builder myDialogBox = new AlertDialog.Builder(mainContext);
                myDialogBox.setTitle("扫码提示");
                myDialogBox.setMessage("是否核销？");
                myDialogBox.setCancelable(false);
                myDialogBox.setPositiveButton("确认", new DialogInterface.OnClickListener(){

                    public void onClick(DialogInterface dialog, int whichButton) {
                        Map<String,String> map = new HashMap<>();
                        map.put("ikey",reserveInfo.getIkey());
                        map.put("awardID",reserveInfo.getAwardID());
                        map.put("appID",reserveInfo.getAppID());
                        map.put("cemID",reserveInfo.getCemID());
                        map.put("qrtime",reserveInfo.getQrtime());
                        ApiClient.INSTANCE.getInstance().orderquanyiCheck(map).
                                subscribeOn(Schedulers.io())
                                .subscribeOn(AndroidSchedulers.mainThread())
                                .subscribe(new ApiCallback<BaseResp<Object>>() {
                                    @Override
                                    protected void onSuccess(BaseResp<Object> t) {
                                        Toast.makeText(MyScanActivity.this, "核销成功", Toast.LENGTH_LONG).show();
                                    }

                                    @Override
                                    protected void onError(@NotNull ApiException ex) {

                                    }
                                });
                        dialog.dismiss();
                    }

                });
                myDialogBox.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                AlertDialog alertDialog = myDialogBox.create();
                alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                alertDialog.show();
            }
        });
    }



    private void doQuanyiCheck(){
        Dialog dialog = new AlertDialog.Builder(this).setCancelable(true).setTitle("扫码提示")
                .setMessage("是否核销？")
                .setPositiveButton("确认",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Map<String,String> map = new HashMap<>();
                                map.put("ikey",reserveInfo.getIkey());
                                map.put("awardID",reserveInfo.getAwardID());
                                map.put("appID",reserveInfo.getAppID());
                                map.put("cemID",reserveInfo.getCemID());
                                map.put("qrtime",reserveInfo.getQrtime());
                                ApiClient.INSTANCE.getInstance().orderquanyiCheck(map).
                                        subscribeOn(Schedulers.io())
                                        .subscribeOn(AndroidSchedulers.mainThread())
                                        .subscribe(new ApiCallback<BaseResp<Object>>() {
                                            @Override
                                            protected void onSuccess(BaseResp<Object> t) {
                                                Toast.makeText(MyScanActivity.this, "核销成功", Toast.LENGTH_LONG).show();
                                            }

                                            @Override
                                            protected void onError(@NotNull ApiException ex) {

                                            }
                                        });
                                dialog.dismiss();
                            }
                        }).setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                dialog.dismiss();
                            }
                        }).create();
        dialog.show();

    }
}
